﻿using System;
using System.Collections.Generic;
using System.Timers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace kb_24_the_game
{
    public class Obstakel : ISprite
    {
        #region variabele declaratie

        public Texture2D texture;

        public GraphicsDevice graphics { get; set; }

        public SpriteBatch spriteBatch { get; set; }

        public ContentManager content { get; set; }

        public Vector2 position { get; set; }

        private int valTimer = 0;
        public int valTijd;
        private Vector2 startPosition;
        public int type;
        private Vector2 blockOffsetX;
        private Vector2 blockOffsetY;
        public List<Rectangle> colliderRect;
        public int snelheid;
        private SpriteFont font;
        public string textureLocation;
        public Rectangle unitTestRect;
        #endregion variabele declaratie
        //
        public Obstakel(ContentManager content, int type, Vector2 position, int tijd, string textureLocation)
        {
            this.content = content;
            this.type = type;
            this.position = position;
            texture = content.Load<Texture2D>(textureLocation);
            startPosition = position;
            snelheid = 3;
            valTijd = tijd;
            colliderRect = new List<Rectangle>();
            this.textureLocation = textureLocation;
        }

        public Obstakel(ContentManager content, int type, Vector2 position, string textureLocation)
        {
            this.content = content;
            this.type = type;
            this.position = position;
            texture = content.Load<Texture2D>(textureLocation);
            snelheid = 3;
            font = content.Load<SpriteFont>("AlgemeneTextures/tussenSchermTekst");
            colliderRect = new List<Rectangle>();
            this.textureLocation = textureLocation;
        }

        public Obstakel()
        {

        }

        public void Update(GameTime gameTime)
        {
            valTimer++;
        }

        public void beweegLinks()
        {
            position += new Vector2(snelheid, 0);
        }

        public void beweegRechts()
        {
            position -= new Vector2(snelheid, 0);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (textureLocation != "Level1Textures/block")
            {
                DrawItem(spriteBatch);
            }

            if (type == 1)
            {
                drawStairsLeft(spriteBatch);
                blockOffsetX.X = texture.Width;
                blockOffsetY.Y = texture.Height;
            }
            else if (type == 2)
            {
                drawStairsRight(spriteBatch);
                blockOffsetX.X = texture.Width;
                blockOffsetY.Y = texture.Height;
            }
        }

        private void DrawItem(SpriteBatch spriteBatch)
        {
            colliderRect.Clear();
            spriteBatch.Draw(texture, position, Color.White);
            Rectangle rect1 = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            colliderRect.Add(rect1);
        }


        public void drawStairsLeft(SpriteBatch spriteBatch)
        {
            colliderRect.Clear();
            //linker blok
            spriteBatch.Draw(texture, position, Color.White);
            Rectangle rect1 = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            colliderRect.Add(rect1);
            //Midden rij
            spriteBatch.Draw(texture, position + blockOffsetX, Color.White);
            spriteBatch.Draw(texture, position + blockOffsetX - blockOffsetY, Color.White);
            Rectangle rect2 = new Rectangle((int)position.X + (int)texture.Width, (int)position.Y, texture.Width, texture.Height);
            Rectangle rect3 = new Rectangle((int)position.X + (int)texture.Width, (int)position.Y - (int)texture.Height, texture.Width, texture.Height);
            colliderRect.Add(rect2);
            colliderRect.Add(rect3);
            //Rechter rij
            spriteBatch.Draw(texture, position + (blockOffsetX * 2), Color.White);
            spriteBatch.Draw(texture, position + (blockOffsetX * 2) - blockOffsetY, Color.White);
            spriteBatch.Draw(texture, position + (blockOffsetX * 2) - (blockOffsetY * 2), Color.White);
            Rectangle rect4 = new Rectangle((int)position.X + ((int)texture.Width * 2), (int)position.Y, texture.Width, texture.Height);
            Rectangle rect5 = new Rectangle((int)position.X + ((int)texture.Width * 2), (int)position.Y - (int)texture.Height, texture.Width, texture.Height);
            Rectangle rect6 = new Rectangle((int)position.X + ((int)texture.Width * 2), (int)position.Y - ((int)texture.Height * 2), texture.Width, texture.Height);
            colliderRect.Add(rect4);
            colliderRect.Add(rect5);
            colliderRect.Add(rect6);
        }

        public void drawStairsRight(SpriteBatch spriteBatch)
        {
            colliderRect.Clear();
            //linker rij
            spriteBatch.Draw(texture, position, Color.White);
            spriteBatch.Draw(texture, position - blockOffsetY, Color.White);
            spriteBatch.Draw(texture, position - (blockOffsetY * 2), Color.White);
            Rectangle rect1 = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            Rectangle rect2 = new Rectangle((int)position.X, (int)position.Y - (int)texture.Height, texture.Width, texture.Height);
            Rectangle rect3 = new Rectangle((int)position.X, (int)position.Y - ((int)texture.Height * 2), texture.Width, texture.Height);
            colliderRect.Add(rect1);
            colliderRect.Add(rect2);
            colliderRect.Add(rect3);
            //Midden rij
            spriteBatch.Draw(texture, position + blockOffsetX, Color.White);
            spriteBatch.Draw(texture, position + blockOffsetX - blockOffsetY, Color.White);
            Rectangle rect4 = new Rectangle((int)position.X + (int)texture.Width, (int)position.Y, texture.Width, texture.Height);
            Rectangle rect5 = new Rectangle((int)position.X + (int)texture.Width, (int)position.Y - (int)texture.Height, 50, texture.Height);
            colliderRect.Add(rect4);
            colliderRect.Add(rect5);
            //rechter rij
            spriteBatch.Draw(texture, position + (blockOffsetX * 2), Color.White);
            Rectangle rect6 = new Rectangle((int)position.X + ((int)texture.Width * 2), (int)position.Y, texture.Width, texture.Height);
            colliderRect.Add(rect6);
        }

        public void ValObstakel(int offset)
        {
            if (valTimer >= valTijd)
            {
                position += new Vector2(0, snelheid);
                if (position.Y >= 475)
                {
                    ResetVal(offset);
                }
            }
        }

        public void ResetVal(int offset)
        {
            valTimer = 0;
            position = new Vector2(startPosition.X + offset, startPosition.Y);
        }
    }
}
